package com.hntxk.cqzd.org.action;

import com.hntxk.core.util.StringUtil;
import com.hntxk.sp.common.action.SpBaseController;
import com.hntxk.sp.common.model.TreeNode;
import com.hntxk.sp.constant.ModuleSP;
import com.hntxk.sp.framework.model.AppApplication;
import com.hntxk.sp.framework.service.IApplicationService;
import com.hntxk.sp.framework.util.MenuTreeUtil;
import com.hntxk.sp.org.model.OrgOrganization;
import com.hntxk.sp.util.TreeUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.logging.log4j.LogManager;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;



/**
 * 组织机构controller类
 * 
 * @author Huang.Biquan
 * @date 2016年10月17日 下午6:11:52
 */
@Api(value="组织机构查询器", tags={"[SP]组织机构查询器"})
@Controller
@RequestMapping("cqzd/do/org/organization")
public class OrgController extends SpBaseController {

	private final IApplicationService applicationService;

	public OrgController(@Qualifier("applicationService")  IApplicationService applicationService) {
		super(ModuleSP.ORG_ORGANIZATION, applicationService, LogManager.getLogger(OrgController.class));
		this.applicationService=applicationService;
	}

	/**
	 *  获取指定应用下的所有组织机构
	 *
	 * @param idOrCode 应用ID或应用编码
	 * @return
	 */
	@ApiOperation(value="获取指定应用下的所有组织机构",notes = "API接口")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "idOrCode", value = "应用ID或应用编码",required = true, example = " or sp", dataTypeClass = String.class),
			@ApiImplicitParam(name = "containEmptyOrg", value = "是否包含空的组织结构", example = "true", dataTypeClass = String.class)
	})
	@RequestMapping(value = "/listAllTreeNode/{idOrCode}",method = {RequestMethod.GET,RequestMethod.POST},produces= PRODUCE_UTF8_JSON)
	@ResponseBody
	public String listAllTreeNode(@PathVariable String idOrCode,
								  @RequestParam(required = false) String containEmptyOrg) {

		try {
			Long applicationId;
			AppApplication application = applicationService.getByCode(idOrCode);
			if (null != application){
				applicationId = application.getAppid();
			}else{
				try {
					applicationId = Long.parseLong(idOrCode);
				} catch (Exception ignore){
					return toJson(null);
				}
			}

			OrgOrganization organization = new OrgOrganization();
			organization.getProp().put("applicationId",applicationId);
			List<TreeNode> nodes = applicationService.query(TreeNode.class,"com.hntxk.cqzd.org.mapper.OrgMapper.".concat("listAllTreeNode"), organization.getProp());
			String isJsTree = "jsTree";
			if (StringUtil.isNotNull(request.getParameter(isJsTree))){
				//jsTree
				return MenuTreeUtil.convertToJsTreeString(nodes);
			}else{
				if(null == containEmptyOrg){
					//清空无子节点Node
					TreeUtil.clearNoChildNode(nodes,"organization");
				}

				//miniUI
				return toJson(nodes,null);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return getError(e);
		}
	}

}
